package com.eascs.ticket.user.controller;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.common.assertion.util.AssertInfoUtils;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.common.form.CodeForm;
import com.eascs.ticket.user.entity.UserStructure;
import com.eascs.ticket.user.form.SaveParentForm;
import com.eascs.ticket.user.service.UserService;
import com.eascs.ticket.user.service.UserStructureService;

@RestController
public class UserStructureRestController extends BaseRestController {

	@Inject
	private UserStructureService userStructureService;

	@Inject
	private UserService userService;

	@RequestMapping(value = "/user/structure/getUpdateParentView")
	public HtmlRestResult getUpdateParentView(CodeForm codeForm) {
		ViewContext.put("user", userService.getUserByCode(codeForm.getCode()));
		return HtmlRestResult.popHtmlFragment("user/structure/getUpdateParentView");
	}

	@RequestMapping(value = "/user/structure/saveParent")
	public BaseRestResult<String> saveParent(SaveParentForm saveParentForm) {
		UserStructure userStructure = new UserStructure();
		userStructure.setUserCode(saveParentForm.getUserCode());
		userStructure.setParentUserCode(saveParentForm.getParentUserCode());
		AssertInfoUtils.assertTrue(!userStructure.getUserCode().equals(userStructure.getParentUserCode()), "父结点与子结点不能为同一个人");
		EntityUtil.initInsertEntity(userStructure);
		userStructureService.insertUserStructure(userStructure);
		return BaseRestResult.refresh();
	}
}
